<?php
declare (strict_types = 1);

namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class SynDb extends Command
{   public $output;
    protected function configure()
    {
        // 指令配置
        $this->setName('syndb')
            ->setDescription('the do_run command');
    }
    protected function execute(Input $input, Output $output) {
        $this->output = $output;
        $output->writeln("\033[32;40m开始执行同步定时任务表\033[0m");
        $config = config('database.connections.mysql');
        //创建表名
        $task_table = $config['prefix'].'task';
        Db::execute($this->createJobTable($task_table));
        try {
            Db::name('task')->find();
        } catch (\Exception $e) {
            $this->outputFail('同步定时任务表失败');
        }
        $task_table = $config['prefix'].'task_log';
        Db::execute($this->createTaskLogTable($task_table));
         try {
            Db::name('task_log')->find();
        } catch (\Exception $e) {
            $this->outputFail('同步定时任务日志表失败');
        }
        $this->createConf() ?$this->outputOK('同步成功') :$this->outputFail('创建配置失败');;
    }
    //创建任务表
    private  function   createJobTable($table){
        $sql = <<<SQL
        CREATE TABLE IF NOT EXISTS `{$table}`  (
         `id` int(11) NOT NULL AUTO_INCREMENT,
          `name` varchar(255) NOT NULL DEFAULT '',
          `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 禁用 1暂停 2启用',
          `rule` char(100) NOT NULL DEFAULT '' COMMENT '定时任务规则',
          `type` char(20) NOT NULL COMMENT '1 执行http请求 2 执行shell 3.mysql',
          `remark` varchar(255) CHARACTER SET utf8mb4 NOT NULL COMMENT '任务备注',
          `create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
          `update_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间',
          `time_out` int(3) NOT NULL DEFAULT '5' COMMENT '执行超时时间',
          `command` varchar(255) NOT NULL DEFAULT '' COMMENT '执行命令',
          `params` varchar(255) DEFAULT NULL COMMENT '执行参数',
          `http_method` tinyint(1) DEFAULT NULL COMMENT 'http 执行方式 1 get 2. post',
          `delete_time` int(11) NOT NULL DEFAULT '0',
          PRIMARY KEY (`id`) USING BTREE
        ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '定时器任务表' ROW_FORMAT = DYNAMIC;
SQL;
        return $sql;
    }
    //创建任务日志表
    private  function   createTaskLogTable($table){
        $sql = <<<SQL
        CREATE TABLE IF NOT EXISTS `{$table}`  (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `tid` int(11) NOT NULL COMMENT '任务id',
          `status_code` int(4) NOT NULL COMMENT '使用http状态码  200 成功  其他失败',
          `result_content` varchar(255) DEFAULT NULL COMMENT '返回结果',
          `create_time` int(11) NOT NULL COMMENT '创建时间',
          PRIMARY KEY (`id`) USING BTREE
        ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '任务执行日志表' ROW_FORMAT = DYNAMIC;
SQL;
        return $sql;
    }
    //写入配置文件
    private  function   createConf(){
        $config = config('database.connections.mysql');
        $db_config='$db_config';
        $conf = <<<CONF
<?php
    global $db_config;
    $db_config = [
        'host'=> '127.0.0.1',
        'port'=> '3306',
        'user'=> 'root',
        'password'=> 'root',
        'database'=> 'job',
    ];
CONF;
        return file_put_contents(root_path()."\\extend\\crontab\\db_config.php",$conf);
    }


    private  function   outputOk($txt){
        $this->output->writeln("\033[32;40m{$txt}\033[0m");exit;
    }

    private  function   outputFail($txt){
        $this->output->writeln("\033[31;40m{$txt}\033[0m");exit;
    }
}
